﻿using Gfw.BLL.Basic.Account;
using Gfw.BLL.Basic.Manage;
using Gfw.Web.Areas.Admin.Models;
using Gfw.Web.Common;
using Gfw.Web.Models.Manage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Gfw.Web.Areas.Admin.Controllers
{
    [LAuthorize(RequireRole="Cw")]
    public class StatisticsController : Controller
    {
        private const string PAGEINDEX_PROFIT = "PageIndex_Profit";
        private const string QUERYMODEL_PROFIT = "QueryModel_Profit";

        #region Profit

        /// <summary>
        /// 总利润查询
        /// </summary>
        /// <returns></returns>
        public ActionResult ProfitIndex(CommonQueryModel model)
        {
            TempData[QUERYMODEL_PROFIT] = model;
            TempData[PAGEINDEX_PROFIT] = 1;

            ViewBag.Profit = SystemAccountLogic.QueryProfit();
            ViewBag.Bonus = SystemAccountLogic.QueryBonus();
         

            return View(model);
        }

        /// <summary>
        /// 返回指定页面
        /// </summary>
        /// <param name="page">第一页开始计算</param>
        /// <returns></returns>
        public ActionResult ProfitList(int? page)
        {
            try
            {
                if (TempData[PAGEINDEX_PROFIT] == null)
                    TempData[PAGEINDEX_PROFIT] = 1;

                int totalSize, index = page ?? (int)TempData[PAGEINDEX_PROFIT];

                if (index < 1)
                    index = 1;

                CommonQueryModel queryModel = TempData[QUERYMODEL_PROFIT] as CommonQueryModel;
                if (queryModel == null)
                    return HttpNotFound();

                IList<ProfitStatisticsModel> list = StatisticsLogic.ProfitQuery(queryModel.StartDate, queryModel.EndDate, index - 1, ViewHelper.PageSize, out totalSize);
                ViewBag.TotalSize = totalSize;
                TempData[PAGEINDEX_PROFIT] = ViewBag.PageIndex = index;
                TempData[QUERYMODEL_PROFIT] = queryModel;

                // 以下为合计，显示在表格最后一行！
                decimal _annualFee = 0, _sellStock = 0, _toCashTax = 0, _tax = 0, _changeInformation = 0, _total = 0,
                    _bonus = 0, _others = 0, _balance = 0;
                string _abstract = "合计",
                    _settlementDate = string.Format("{0}~~{1}", queryModel.StartDate.Value.ToString("yyyy/MM/dd"), queryModel.EndDate.Value.ToString("yyyy/MM/dd"));
                foreach (ProfitStatisticsModel item in list)
                {
                    _annualFee += item.AnnualFee;
                    _sellStock += item.SellStock;
                    _toCashTax += item.ToCashTax;
                    _tax += item.Tax;
                    _changeInformation += item.ChangeInformation;
                    _total += item.Total;
                    _bonus += item.Bonus;
                    _others += item.Others;
                    _balance += item.Balance;
                }

                ViewBag.SettlementDate = _settlementDate;
                ViewBag.AnnualFee = _annualFee.ToString("C");
                ViewBag.SellStock = _sellStock.ToString("C");
                ViewBag.ToCashTax = _toCashTax.ToString("C");
                ViewBag.Tax = _tax.ToString("C");
                ViewBag.ChangeInformation = _changeInformation.ToString("C");
                ViewBag.Total = _total.ToString("C");
                ViewBag.Bonus = _bonus.ToString("C");
                ViewBag.Others = _others.ToString("C");
                ViewBag.Balance = _balance.ToString("C");
                ViewBag.Abstract = _abstract;

                ViewBag.Start = queryModel.StartDate;
                ViewBag.End = queryModel.EndDate;

                return PartialView(list);
            }
            catch (System.Exception ex)
            {
                return HttpNotFound(ex.Message);
            }
        }


        public ActionResult DownloadExcel(DateTime start,DateTime end)
        {
            int totalSize;
            IList<ProfitStatisticsModel> list = StatisticsLogic.ProfitQuery(start, end, 0, 10000, out totalSize);
            return new ExcelActionResult<ProfitStatisticsModel>(list);
        }
        #endregion


        #region Money

        /// <summary>
        /// 会员费用统计
        /// </summary>
        /// <returns></returns>
        public ActionResult MoneyIndex()
        {
            return View();
        }

        #endregion
    }
}
